package com.example.demo_widget.rerecyclerview;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo_widget.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/10
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    public static final String  TAG = "MyAdapter";
    private List<PersonEntity>  mData = new ArrayList<>();
    public void setData(List<PersonEntity> list){
        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 局部刷新:不刷新全局数据，只刷新一个条目
     */
    public void addEntity(int position, PersonEntity entity){
        mData.add(position,entity);
        notifyItemInserted(position);
    }
    public void removeEntity(int position){
        mData.remove(position);
        notifyItemRemoved(position);
    }
    public void changeEntity(int position,String name){
        PersonEntity personEntity = mData.get(position);
        personEntity.setName(name);
        notifyItemChanged(position);
    }

    /**
     * 局部增量刷新：不是刷新全部，不刷新整个条目，刷新一个条目上某个控件
     */

    public void like(int position){
        PersonEntity personEntity = mData.get(position);
        boolean like = personEntity.getLike();
        personEntity.setLike(!like);
//        notifyItemChanged(position); //刷新整个条目 public void onBindViewHolder(@NonNull MyViewHolder holder, int position)
        notifyItemChanged(position,"like"); //只刷新点赞图标
    }




    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: ");
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull List<Object> payloads) {
        if(!payloads.isEmpty() && payloads.get(0).equals("like")){
            Log.d(TAG, "onBindViewHolder: 刷新点赞图标");
            //点赞图标
            if(mData.get(position).getLike()){
                holder.ivLike.setImageResource(R.mipmap.icon_like_checked);
            }else{
                holder.ivLike.setImageResource(R.mipmap.icon_like);
            }
        }else{
            super.onBindViewHolder(holder, position, payloads);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: 刷新整个条目"+position);
        holder.textView.setText(mData.get(position).getName());
        holder.imageView.setImageResource(R.mipmap.ic_launcher_round);
        //点赞图标
        if(mData.get(position).getLike()){
            holder.ivLike.setImageResource(R.mipmap.icon_like_checked);
        }else{
            holder.ivLike.setImageResource(R.mipmap.icon_like);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        private ImageView imageView;
        private ImageView ivLike;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
            imageView = itemView.findViewById(R.id.iv);
            ivLike = itemView.findViewById(R.id.iv_like);
        }
    }
}
